package homework.ATM;

import java.util.Scanner;
/**
 * ATM机流程控制类
 */
public class Control {
    private User[] users = {
            new User("aaaaaa","111111",1000.0),
            new User("bbbbbb","222222",2000.0),
            new User("cccccc","333333",3000.0),
            new User("dddddd","444444",4000.0),
            new User("eeeeee","555555",5000.0),
    };
    private Scanner scan = new Scanner(System.in);
    private int index;
    // 控制ATM流程的方法
    public  void process() {
        System.out.println("欢迎使用xx银行");
        while (true) {
            login();
            menu();
        }
    }
 //    登录方法
    public void login() {
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入您的账号");
            String account = scan.next();
            System.out.println("请输入您的密码");
            String pwd = scan.next();
            for (int j = 0; j < users.length; j++) {
                if (account.equals(users[j].getAccount()) && pwd.equals(users[j].getPwd())) {
                    System.out.println("登陆成功");
                    index = j;
                    return;
                }
            }
            System.out.println("账号或密码错误，请重新输入，还剩"+(2 - i)+"次");
        }
        System.out.println("次数用完，卡已被吞！");
        System.exit(0); // 系统结束
    }
    //    菜单界面方法
    public  void menu() {
        while (true) {
            System.out.println("请选择操作：1-存款 2-取款 3-查询 4-转账 5-切换账号 6-退出 7-更改密码");
            int input = scan.nextInt();
            if (input == 1) {
                save();
            } else if (input == 2) {
                get();
            } else if (input == 3) {
                search();
            } else if (input == 4) {
                transfer();
            } else if (input == 5) {
                System.out.println("退出登录");
                return;
            } else if (input == 6) {
                System.out.println("退出程序");
                System.exit(0);
            } else if (input == 7) {
                if (change()) {
                    System.out.println("已登出");
                    return;
                }
            } else {
                System.out.println("输入错误，请重新选择");
            }
        }
    }
    //    存款方法
    public void save() {
        System.out.println("请输入存款金额：");
        double money = scan.nextDouble();
        users[index].setBalance(users[index].getBalance() + money);
        System.out.println("存入后金额为" + users[index].getBalance());
    }
        //    取款方法
    public void get () {
            System.out.println("请输入取款金额");
            double money = scan.nextDouble();
            if (users[index].getBalance() > money) {
                users[index].setBalance(users[index].getBalance() - money);
                System.out.println("取款成功，当前余额为 " + users[index].getBalance());
            } else {
                System.out.println("当前余额不足，取款失败");
            }
    }
    //    查询方法
    public  void search() {
        System.out.println("您的账户余额为 " + users[index].getBalance());
    }

    //    转账方法
    public  void transfer() {
        System.out.println("请输入对方账号");
        String account = scan.next();
        if (users[index].getAccount().equals(account)) {
            System.out.println("不能给自己转账，请重新选择");
            return;
        }
        for (int j = 0; j < users.length; j++) {
            if (users[j].getAccount().equals(account)) {
                System.out.println("请输入转账金额");
                double money = scan.nextDouble();
                for (int k = 0; k < users.length; ) {
                    if (users[index].getBalance() < money) {
                        System.out.println("对不起，您的余额不足，无法转账");
                        return;
                    } else {
                        users[index].setBalance(users[index].getBalance() - money);
                        users[j].setBalance(users[j].getBalance() + money);
                        System.out.println("转账成功，当前余额为 " + users[index].getBalance());
                        return;
                    }
                }
            }
        }
        System.out.println("输入账号有误，请重新转账");
    }

    //    更改密码的方法
    public  boolean change(){
        System.out.println("请输入原密码");
        String pwd = scan.next();
        if (!pwd.equals(users[index].getPwd())){
            System.out.println("密码输入错误输入");
            return false;
        }
        System.out.println("请输入新密码");
        String newpwd1 = scan.next();
        if (newpwd1.equals(pwd)){
            System.out.println("新密码不能与原密码相同");
            return false;
        }
        System.out.println("请再次输入新密码");
        String newpwd2 = scan.next();
        if (newpwd1.equals(newpwd2)) {
            users[index].setPwd(newpwd1);
            System.out.println("密码修改成功请重新登录");
            return true;
        } else {
            System.out.println("两次新密码输入不相同，请重新输入");
            return false;
        }
    }
    public static void main(String[] args) {
        Control control = new Control();
        control.process();
    }
}

